local love = require 'love'
local fontHandler = require('assets-handler.font-handler')

local setColor = love.graphics.setColor
local polygon = love.graphics.polygon
local drawRectangle = love.graphics.rectangle
local setLineWidth = love.graphics.setLineWidth
local printg = love.graphics.print

local width = Width
local height = Height
local padding = Padding

-- 重排序按钮使用绿色
local reorderButton = {
    x = 0,
    y = 0,
    width = 0,
    height = 0,
    fontSize = 18,
    reorderText = "Ⅲ",
    reorderTextWidth = 0,
    reorderTextHeight = 0,
    textX = 0,
    textY = 0,
    showIcon = false, -- 显示图标或文字
    iconX = 0,
    iconY = 0,
    iconWidth = 0,
    iconHeight = 0,
    -- 排序按钮使用绿色
    normalColor = {0.298, 0.686, 0.314, 1.0}, -- #4CAF50(草绿色)
    hoveredColor = {0.4, 0.733, 0.416, 1.0}, -- #66BB6A(淡草绿)
    currentColor = {},
    redTriangle = {
        normalColor = {0.898, 0.224, 0.208, 1.0}, -- #e53935(深红色)
        hoveredColor = {0.937, 0.325, 0.314, 1.0}, -- #ef5350(亮红色)
        currentColor = {}
    },
    blackTriangle = {
        normalColor = {0.129, 0.129, 0.129, 1.0}, -- #212121(深灰色)
        hoveredColor = {0.259, 0.259, 0.259, 1.0}, -- #424242(中灰色)
        currentColor = {}
    },
    whiteFrame = {
        normalColor = {0.961, 0.961, 0.961, 1.0}, -- #f5f5f5(浅灰色)
        hoveredColor = {0.878, 0.878, 0.878, 1.0}, -- #e0e0e0(中浅灰色)
        currentColor = {}
    },
    isHovered = false
}

local function init()
    local circleRadius = height / 20
    local widthButton = circleRadius
    local heightButton = circleRadius
    reorderButton.widthButton = circleRadius
    reorderButton.heightButton = circleRadius
    reorderButton.x = width / 2 - circleRadius - widthButton
    reorderButton.y = height - padding * 2 - heightButton
    reorderButton.width = widthButton
    reorderButton.height = heightButton
    -- 出牌圆形按钮文字
    fontHandler.setFont(reorderButton.fontSize)
    reorderButton.reorderTextWidth = fontHandler.getFontWidth(reorderButton.reorderText)
    reorderButton.reorderTextHeight = fontHandler.getFontHeight()
    -- 排序按钮使用绿色
    reorderButton.currentColor = reorderButton.normalColor
    reorderButton.redTriangle.currentColor = reorderButton.redTriangle.normalColor
    reorderButton.blackTriangle.currentColor = reorderButton.blackTriangle.normalColor
    reorderButton.whiteFrame.currentColor = reorderButton.whiteFrame.normalColor

    reorderButton.iconX = reorderButton.x + reorderButton.width * 0.2
    reorderButton.iconY = reorderButton.y + reorderButton.height * 0.2
    reorderButton.iconWidth = reorderButton.width * 0.6
    reorderButton.iconHeight = reorderButton.height * 0.6

    reorderButton.textX = reorderButton.x + reorderButton.width / 2 - reorderButton.reorderTextWidth / 2
    reorderButton.textY = reorderButton.y + reorderButton.height / 2 - reorderButton.reorderTextHeight / 2 + 4
end

local function drawIcon(x, y, IconWidth, IconHeight)
    -- 设置线宽
    setLineWidth(1)

    -- 绘制红色三角形（右上）
    setColor(reorderButton.redTriangle.currentColor)
    polygon("fill",
        x, y,                 -- 左上角
        x + IconWidth, y,         -- 右上角
        x + IconWidth, y + IconHeight -- 右下角
    )

    -- 绘制黑色三角形（左下）
    setColor(reorderButton.blackTriangle.currentColor)
    polygon("fill",
        x, y,                  -- 左上角
        x + IconWidth, y + IconHeight, -- 右下角
        x, y + IconHeight          -- 左下角
    )

    -- 绘制白色边框
    setColor(reorderButton.whiteFrame.currentColor)
    drawRectangle("line", x, y, IconWidth, IconHeight)
end

local function drawButton()
    -- 更新按钮颜色
    if reorderButton.isHovered then
        reorderButton.currentColor = reorderButton.hoveredColor
        reorderButton.redTriangle.currentColor = reorderButton.redTriangle.hoveredColor
        reorderButton.blackTriangle.currentColor = reorderButton.blackTriangle.hoveredColor
        reorderButton.whiteFrame.currentColor = reorderButton.whiteFrame.hoveredColor
    else
        reorderButton.currentColor = reorderButton.normalColor
        reorderButton.redTriangle.currentColor = reorderButton.redTriangle.normalColor
        reorderButton.blackTriangle.currentColor = reorderButton.blackTriangle.normalColor
        reorderButton.whiteFrame.currentColor = reorderButton.whiteFrame.normalColor
    end

    -- 绘制按钮
    setColor(reorderButton.currentColor)
    drawRectangle("fill", reorderButton.x, reorderButton.y, reorderButton.width, reorderButton.height, 10, 10)

    -- 绘制按钮边框
    setColor(0.2, 0.2, 0.2, 1)
    setLineWidth(1)
    drawRectangle("line", reorderButton.x, reorderButton.y, reorderButton.width, reorderButton.height, 10, 10)

    -- 绘制文字或图标
    if reorderButton.showIcon then
        drawIcon(reorderButton.iconX, reorderButton.iconY, reorderButton.iconWidth, reorderButton.iconHeight)

    else
        fontHandler.setFont(reorderButton.fontSize)
        setColor(0, 0, 0)
        printg(reorderButton.reorderText, reorderButton.textX, reorderButton.textY)
    end
end

local function inArea(x, y)
    return x >= reorderButton.x and x <= reorderButton.x + reorderButton.width and
            y >= reorderButton.y and y <= reorderButton.y + reorderButton.height
end

local function isHovered(boolean)
    reorderButton.isHovered =  boolean
end

local function isPressed()
    reorderButton.showIcon = not reorderButton.showIcon
end

return{
    init = init,
    drawButton = drawButton,
    inArea = inArea,
    isHovered = isHovered,
    isPressed = isPressed,
    getShowIcon = function() return reorderButton.showIcon end
}